<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Base extends Controller
{
    public function initialize()
    {
        header('Allow: GET, OPTIONS, POST');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:cache-control, X-Requested-With');

        if (input('?param.user_token')) {
            $this->AutoLogin(input('param.user_token'));
        }


        if (request()->isOptions()) {
            exit;
        }
        
        if (IsLogin() == false) {
            if (request()->path() != 'admin/user/login') {
                exit(json_encode(Result(-1, '请登录后再进行操作')));
            }
        }
    }

    /**
     * 利用token自动登录
     */
    public function AutoLogin($token)
    {
        $rsa = new \RSA\RSA();
        $user_id = $rsa->Decode($token);

        if ($user_id != null && $user_id == 'Administrator') {
            $GLOBALS['user_status'] = 1;
            $GLOBALS['user_id'] = $user_id;
        }
    }

}
